<?php

namespace Addons\RedPackets\Model;

use Think\Model;

/**
 * Class RedPackets
 * @package Addons\RedPackets\Model 
 * @author:温作权 发红包功能
 */
class AddonRedPacketsModel extends \BaseModel
{
 
    /*****发红包**/
    public function add($row)
    {

					   if(!intval($row['user_id'])){
								  return array('error'=>1,'msg'=>'用户Id不能空');
								}
								
								if(!is_numeric($row['num'])){
								  return array('error'=>2,'msg'=>'红包金额不合法');
								}
								
        $data=array(
								  'title'=>$row['title'],
										'num'=>$row['num'],
										'add_time'=>time(),
										'from_user_id'=>intval($row['from_user_id']),
										'user_id'=>intval($row['user_id']),
										'title'=>$row['title'],
										'status'=>'1',
								);
        $id = parent::add($data);
								//发红包消息
								if($id){
									 $params=array(
														'user_id'=>$row['user_id'],
														'content'=>$row['title'],
										);
									 Logic("BoxSns","Noti")->send("",$params);
								}
        return array('error'=>'0','msg'=>'成功','id'=>$id);
    }
				
				
				//领取红包
				public function  getRedPackets($id){
				 	$data=BoxModel("addon_red_packets")->where(array('id'=>$id))->find();
					 $r=BoxModel("addon_red_packets")->where(array('id'=>$id))->save(array('status'=>2));
						/******领取经包***/
						if($r){
							    $row=array(
											   'user_id'=>$data['user_id'],
														'num'=>$data['num'],
														'remark'=>$data['title'],
														'type'=>'money',
											);
											$param=json_decode($data['params'],true);
											if(!empty($param)){
											  	$row=array_merge($row,$param);
											}
											Logic("BoxSns","Log")->add($row);
											return array('error'=>0,'msg'=>'领取成功');
						}else{
						    return array('error'=>1,'msg'=>'领取失败');
						}
						 
				}
 

}
